import { http } from "@/utils/http";

export type LoginResult<T = number> = {
  /** 权限 token */
  access_token: string;
  /** 刷新 token */
  refresh_token: string;
  /** token前缀 */
  token_type: string | "bearer";
  /** 用户名 */
  user_name: string;
  /** 用户 ID */
  user_id: string;
  /** 过期时间 */
  expires_in: T;
  /** 昵称 */
  nick_name: string;
  /** 真实名称 */
  real_name: string;
  /** 角色名 */
  role_name: string;
};

/**
 *
 * @param account 用户名
 * @param password 密码
 * @param code 验证码
 * @returns
 */
// export function loginApi(account, password, code, tenant_id) {
export function loginApi(account, password, tenant_id) {
  return http.request<LoginResult>("post", "/cupAuthApi/oauth/token", {
    params: {
      username: account.trim(),
      password,
      tenant_id,
      grant_type: "password",
      scope: "all",
      type: "account",
    },
    headers: {
      "Captcha-Key": "",
      // "Captcha-Code": code,
    },
  });
}

/**
 *
 * @param account 用户名
 * @param password 密码
 * @param code 验证码
 * @returns
 */
// export function loginApi(account, password, code, tenant_id) {
export function loginSsoApi() {
  return http.request<LoginResult>("post", "/kg-web/sso/oauth/token", {
    // params: {
    //   grant_type: "cas",
    //   scope: "all",
    //   type: "sso",
    // },
  });
}

/**
 * 刷新 token
 * @param refreshToken
 * @returns
 */
export function refreshTokenApi(refreshToken, tenant_id = "000000") {
  return http.request<LoginResult>("post", "/cupAuthApi/oauth/token", {
    params: {
      grant_type: "refresh_token",
      scope: "all",
      tenant_id,
      refresh_token: refreshToken,
    },
  });
}

/**
 * @description 获取用户信息
 */
export const getUserInfo = () => {
  return http.request("get", "/cupUserApi/info");
};
